
import {playerExportXls,queryAllAddress} from './playerInfoListServ.js'
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
import { message } from 'antd';
import { download, makeAddress } from 'utils/util'
import axios from 'axios'
import config from 'config/config'
const defultState = {
    gameActivityId:'',
    biz:{}
}

// 声明module
const tmpModule = {
    state: cloneDeep(defultState),
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname, query }) => {
                if ('' + tmpModule.pathname === '' + pathname) {
                    //   dispatch({type: 'queryInfo', payload: {}}) // end dispatch
                    dispatch({ type: 'updateStore', payload: cloneDeep(defultState) });
                    dispatch({ type: 'getAreaList', payload: {} })
                    // debugger
                    dispatch({ type: 'updateStore', payload:{gameActivityId:query.gameActivityId} });
                }
            })
        },
    },


    // 方法列表
    effects: {
        *playerExportXls({ payload }, { put, call, select }) {
            axios({
                method: 'post',
                url: config.baseUrl+'mideadc-game-center-service/activity/gameActivityUser/exportXls',
                responseType: 'blob',
                data: {...payload},
                headers: {}
            }).then(response => {

                let fileName = '玩家信息'
                let data = response.data
                // let {data} = yield call(playerExportXls, {...payload})
                if (typeof window.navigator.msSaveBlob !== 'undefined') {
                    window.navigator.msSaveBlob(
                        new Blob([data]),
                        fileName + '.xls'
                    );
                } else {
                    console.log('new Blob([data]',new Blob([data]))
                    let url = window.URL.createObjectURL(new Blob([data]));
                    let link = document.createElement('a');
                    link.style.display = 'none';
                    link.href = url;
                    link.setAttribute('download', fileName + '.xls');
                    document.body.appendChild(link);
                    link.click();
                    document.body.removeChild(link); //下载完成移除元素
                    window.URL.revokeObjectURL(url); //释放掉blob对象
                }
                message.success('操作成功')
                console.log('config',config)
                console.log(window.location.origin)
            })
        },
        		//获取级联列表
		*getAreaList({ payload }, { put, call, select }) {
			try {
			  const { data, success } = yield call(queryAllAddress);
			  if (success) {
				let areaList = [];
				let arr = [data[0]]
				let attr = [data[data.length - 1]]
				data[0] = {
				  label: "北京",
				  value: "110100",
				  children: arr
				}
				data[data.length - 1] = {
				  label: "澳门",
				  value: "820000",
				  children: attr
				}
				//去掉第三级，
				areaList = makeAddress(data, 2);
				
				yield put({ type: 'updateStore', payload: { areaList: areaList } });
			  }
			} catch (e) {
	  
			}
		  },
    },

    // reducers: {
    //   updateStore(preState, action) {
    //     return Object.assign({}, preState, action.payload)
    //   },
    // }
    reducers: {
        updateStore(preState, action) {
            return { ...preState, ...action.payload };
        }
    }
}

export default tmpModule
